import 'package:uikit/http/core/hi_net.dart';
import 'package:uikit/utils/sp_util.dart';

import '../request/base_request.dart';
import '../request/login_request.dart';
import '../request/register_request.dart';

class LoginDao {
  /// 登录令牌
  static const BOARDING_PASS = "boarding-pass";

  static Future login(String userName, String password) {
    return sendRequest(userName, password);
  }

  static Future register(String userName, String password, String imoocId, String orderId) {
    return sendRequest(userName, password, imoocId: imoocId, orderId: orderId);
  }

  static Future sendRequest(String userName, String password, {String? imoocId, String? orderId}) async {
    BaseRequest request;
    if (imoocId != null && orderId != null) {
      request = RegisterRequest();
    } else {
      request = LoginRequest();
    }
    // 添加参数
    request.add("userName", userName);
    request.add("password", password);
    request.add("imoocId", imoocId);
    request.add("orderId", orderId);
    // 发送请求
    final result = await HiNet.instance().fire(request);
    print("登陆注册sendRequest $result");
    if (result["code"] == 0 && result["data"] != null) {
      // 保存登录令牌
      SpUtil.setString(BOARDING_PASS, result["data"]);
    }
    print("登录注册请求结果：$result");
    return result;
  }

  /// 获取登录令牌
  static String getBoardingPass() {
    return SpUtil.getString(BOARDING_PASS);
  }
}
